<template>
  <view class="content">
    <view style="background: #fff">
      <basics-form ref="basicsForm" @submit="submit" :hiddenMustIcon="true" />
    </view>
    <view class="result" v-if="result && result.state == 2">
      <view class="state">审核拒绝：</view>
      <view class="refuse"> {{ result.refuseReason }}</view>
      <view class="time">申请时间：{{ result.applyDate.substring(0, 16) }}</view>
      <view class="time">审核时间：{{ result.approveDate.substring(0, 16) }}</view>
      <view class="tip">您可以重新提交申请</view>
    </view>
    <view style="height: 200rpx" />
    <foot-button
      v-if="btnFlag || (result && result.state == 2)"
      style="position: fixed; bottom: 0rpx; z-index: 10"
      :buttonList="buttonList"
      @tapButton="$refs.basicsForm.formValid()"
    />
  </view>
</template>

<script>
import basicsForm from "@/components/basics-form/basics-form.vue";
import footButton from "@/components/foot-button/foot-button.vue";

export default {
  components: {
    basicsForm,
    footButton,
  },
  data() {
    return {
      doctorList: [],
      buttonList: [
        {
          name: `提交`,
          icon: "tijiao",
        },
      ],
      btnFlag: true,
      patientId: "",
      result: null,
    };
  },
  onShareAppMessage() {
    return this.$util.returnShareObj();
  },
  onLoad(options) {
    this.patientId = options.patientId;
    if (options.details) {
      this.btnFlag = false;
      this.doctorGetChangeDoctorResult();
    } else {
      this.getDdoctorList();
    }
  },
  methods: {
    //审核结果
    doctorGetChangeDoctorResult() {
      this.$api.doctorGetChangeDoctorResult({ patientId: this.patientId }).then((res) => {
        if (res.code == 200) {
          this.result = res.data;
          this.getDdoctorList();
        }
      });
    },
    //医生
    getDdoctorList() {
      this.$api.doctorList().then((res) => {
        if (res.code == 200) {
          this.doctorList = res.data;
          this.initRules();
        }
      });
    },
    initRules() {
      let doctorValue = "",
        reason = "";
      if (this.result) {
        reason = this.result.reason;
        this.doctorList.some((item, index) => {
          if (item.doctorId == this.result.changeDoctorId) {
            doctorValue = index;
          }
        });
      }
      let rules = [
        {
          title: "医生",
          key: "doctorId",
          value: doctorValue,
          type: "select",
          selectList: this.doctorList,
          selectKey: "doctorId",
        },
        {
          title: "更换原因",
          key: "reason",
          type: "textarea",
          value: reason,
        },
      ];
      this.$nextTick(() => {
        this.$refs.basicsForm.initForm(rules);
      });
    },
    submit(param) {
      if (!param) {
        return;
      }
      uni.showModal({
        title: "提示",
        content: "确认提交？",
        success: (res1) => {
          if (res1.confirm) {
            param.patientId = this.patientId;
            this.$api.doctorChangeDoctor(param).then((res) => {
              if (res.code == 200) {
                uni.setStorageSync("refreshMyPatient", true);
                this.$util.showSuccessToast("提交成功，请等待审核", true);
              }
            });
          }
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.content {
  background: $back-grey-color;
  height: 100vh;
  .result {
    padding: 40rpx 40rpx 0 40rpx;
    .state {
      color: #e83112;
      margin-bottom: 20rpx;
    }
    .refuse {
      margin-bottom: 20rpx;
    }
    .time {
      color: $font-grey-color;
      font-size: $small-font-size;
    }
    .tip {
      margin-top: 20rpx;
      color: $theme-color;
      font-size: $small-font-size;
    }
  }
}
</style>
